package com.aaa.controller;

import com.aaa.pojos.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liuyp
 * @since 2025/4/26
 */
@RestController
public class DemoCookieController {

    @GetMapping("/c1")
    public Result c1(HttpServletResponse response){
        //模拟登录成功之后，将当前用户的身份添加到Cookie里。目的是为了在浏览器的后续请求中能共享、获取用户数据
        response.addCookie(new Cookie("id", "1"));
        response.addCookie(new Cookie("username", "tom"));
        return Result.success("用户身份已经添加到Cookie里了");
    }


    @GetMapping("/c2")
    public Result c2(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("name = " + name + ",  value = " + value);
            }
        }
        return Result.success();
    }
}
